package com.yaoli.ajob;

/**
 * Created by will on 2017/4/22.
 */
public class _121_BestTimetoBuyandSellStock {
    public int maxProfit(int[] prices) {
        if(prices.length == 0 || prices.length == 1){
            return 0;
        }

        int maxProfit = 0;
        int minStock_point = 0;
        int temp;

        for(int i = 1 ; i < prices.length ; i ++){
            temp = prices[i] - prices[minStock_point];

            if(temp > 0 && temp > maxProfit){
                maxProfit = temp;
            }

            if(prices[i] < prices[minStock_point] ){
                minStock_point = i;
            }
        }

        return maxProfit;
    }
}
